// JavaScript Document
/***********采纳问题*****/
function accept(obj) {
    var id = $(obj).attr("id");
    if (confirm("确定采纳这个回答吗？")) {
        $.getJSON("/Ask/accept", {id: id}, function (data) {
            alert(data.msg);
            if (data.error == 0) {
                window.location.reload();
            }
        })
    }
}


/******计算价格**/
function get_order_amount() {
    /*******使用卡金额**/
    var yie = $("#yie").val();
    if (!yie) yie = 0;
    var save_money = 0;
    /*******微信优惠码**/
    var card_money = $("#card_num").attr("money");
    if (typeof(card_money) != "undefined" && card_money > 0) {
        card_money = Number(card_money);
        save_money += card_money;
    } else {
        card_money = 0;
    }
    /*******校友优惠码**/
    var school_money = $("#school_num").attr("money");
    if (typeof(school_money) != "undefined" && school_money > 0) {
        school_money = Number(school_money);
        save_money += school_money;
    } else {
        school_money = 0;
    }


    /*****省多少钱*/
    save_money = save_money.toFixed(0);
    if (save_money > 0) {
        $(".save_money").text(save_money);
        $(".save_money").parent().show();
    } else {
        $(".save_money").parent().hide();
    }


    order_amount = goods_amount - yie - school_money - card_money + get_buy_price();
    //	alert(order_amount);
    order_amount = order_amount > 0 ? order_amount : 0;
    order_amount = order_amount.toFixed(0);
    $(".order_amount").text(order_amount);
}

//商品价格
function get_buy_price() {
    /*******加线下课程价格******/
    var offline_subject_price = 0;
    if ($("#buy_select_offline_fee").length > 0 && $("#buy_select_offline_fee").hasClass("buy_select")) {
        offline_subject_price = Number($("#buy_select_offline_fee").attr("price") ? $("#buy_select_offline_fee").attr("price") : 0);
    }
    //在线报名
    var examination_fee = 0;
    if ($("#buy_select_examination_fee").length > 0 && $("#buy_select_examination_fee").hasClass("buy_select")) {
        examination_fee = Number($("#buy_select_examination_fee").attr("price") ? $("#buy_select_examination_fee").attr("price") : 0);
    }
    return offline_subject_price + examination_fee;
}


/******输入卡券**/
function add_num(obj) {
    var name = $(obj).attr("attr");
    $("#" + name + "_input input").show();
    $(obj).hide();
    $("#" + name).hide();
    $("#" + name + "_comfim").css("display", "inline-block");
}


/*********验证卡券****/
function check_num(obj) {
    var name = $(obj).attr("id");
    var num = $(obj).val();
    var check_url = "/Subject/get_card";
    //var subjectid=$("#subjectid").val();
    $.getJSON(check_url, {num: num, type: name, subjectid: subjectid}, function (data) {
        if (data.error == 0) {
            $("#" + name + "_text").find("font").text("优惠￥" + data.money);
            $(obj).attr("money", data.money);
            $("#" + name + "_text").show();
            $(obj).parent().hide();
            get_order_amount();
        } else {
            $("#" + name + "_text").find("font").text("优惠￥0");
            $("#" + name + "_text").hide();
            $(obj).attr("money", "0");
            get_order_amount();
            dialog(data.msg);
        }
    })
}


/********余额***/
function check_yie(obj) {
    var money = Number($(obj).attr("max"));
    if (Number($(obj).val()) > money) {
        $(obj).val(money.toFixed(2));
    }
    var school_money = $("#school_num").attr("money");
    var all_goods_amount = Number(goods_amount) + get_buy_price();
    all_goods_amount = Number(all_goods_amount);
    if (typeof(school_money) != "undefined" && Number(school_money) > 0) {
        all_goods_amount -= school_money;
    }
    if (all_goods_amount < 0) {
        $(obj).val(0);
    } else if (Number($(obj).val()) > all_goods_amount) {
        $(obj).val(all_goods_amount.toFixed(2));
    }
    get_order_amount();
}


function add_buy(_this) {
    var price = Number($(_this).attr("price"));
    if ($(_this).hasClass("buy_select")) {
        $(_this).removeClass("buy_select");
    } else {
        $(_this).addClass("buy_select");
    }
    get_order_amount();
}


//设置学校信息
function set_shcool(user_info) {
    if (!user_info) {
        return false;
    }
    var title = "";
    if (!user_info.school_id) {
        var title = "完善个人信息";
    }
    if (user_info.school_id && user_info.agreement == 0) {
        var title = "使用协议";
    }
    if (title) {
        var w = $(window).width();
        if (w < 600) {
            modal_w = w - 20;
        } else {
            modal_w = "0.8";
        }
        if ($('[data-page="User_students_info"]').length == 0) {
            hiApp.modal(base_url + "/User/students_info", "students_info", title, modal_w, 1);
        }

    }
}

/*********提交订单*****/
function buy_now(type) {

    //hiApp.modal('http://www.boxphp.com/BoxPay/Order/index/order_sn/G8180388279647d.html',"pay_now","",800);
    //	return false;
    if (!hiApp.login("登录购买课程")) {
        return false;
    }


    if (type > 0) {
        buy_type = type;
        //有在线班级可以上

        if (select_offline == 2) {
            select_offline = 3;
        }
        var w_h = $(window).width();
        if (w_h > 700) {
            var w = $(window).width() * 0.8;
        } else {
            w = "1";
        }
        //有在线报名可以选上
        var url = base_url + "/Subject/checkout/type/" + type + "/subjectid/" + subjectid + "/select_offline/" + select_offline + "/select_apply/";
        if (select_apply == 2) {
            hiApp.confirm("您在所的学校可以快速委托报名考试，可否开通在线报名？", function () {
                select_apply = 3;
                hiApp.modal(url + select_apply, "ToPayNow", "", w);
            }, function () {
                hiApp.modal(url + select_apply, "ToPayNow", "", w);
            })
        } else {
            hiApp.modal(url + select_apply, "ToPayNow", "", w);
        }

        return false;
    }
    //必须在线报考
    var examination_id = $("#examination_id").val();
    if ($("#buy_select_examination_fee").length > 0 && $("#buy_select_examination_fee").hasClass("buy_select") && examination_id == 0) {
        dialog("请确认报考信息", function () {
            $('[target="registerForExamination"]').trigger(hiApp.device.click);
        }, 500);
        return false;
    }

    var type = $("[name='buy_type']").val();
    var card_num = $("#card_num").val();
    /*******校友优惠码**/
    var school_num = $("#school_num").val();
    /*******代理优惠码**/
    var agent_num = $("#agent_num").val();
    var yie = $("#yie").val();
    var offline_subject_id = 0;
    if ($("#buy_select_offline_fee").length > 0 && $("#buy_select_offline_fee").hasClass("buy_select")) {
        offline_subject_id = 1;
    }
    $("#next_btn").attr("disabled", "disabled");
    $("#next_btn").text("提交中...");
    $("#next_btn").removeAttr("click");
    $.getJSON("/Subject/order", {
        subjectid: subjectid,
        card_num: card_num,
        school_num: school_num,
        agent_num: agent_num,
        examination_id: examination_id,
        yie: yie,
        type: buy_type,
        offline_subject_id: offline_subject_id
    }, function (data) {
        var w_h = $(window).width();
        if (w_h > 700) {
            var w = $(window).width() * 0.8;
        } else {
            w = "1";
        }
        if (data.error == 0) {
            hiApp.modal(data.msg, "ToPayNow", "", w);
        } else if (data.error == 3) {
            window.location.href = data.msg;
        } else {
            dialog(data.msg);
        }

        if (data.error == 6) {
            set_shcool(data.user_info)
        }


        if (data.error != 0) {
            $("#next_btn").removeAttr("disabled");
            $("#next_btn").text("下一步");
            $("#next_btn").attr("click", "buy_now()");
        }
    })

}


/**********充值******/
function recharge() {
    var money = $("#money").val();
    $.getJSON(recharge_url, {money: money}, function (data) {
        if (data.error == 0) {
            window.location.href = data.msg;
        } else {
            dialog(data.msg);
        }
    })
}


function show_qust(k) {
    $(".all_questions").hide();
    $(".all_questions").eq(k).show();
}


function commit() {
    $(".questions").each(function (i, v) {
        var questionid = $(v).attr("data-qid");
        console.log(questionid);
        $(v).find(".checked").each(function (k, v2) {
            var value = $(v2).attr("key");
            var html = '<input type="hidden" name="questionid[' + questionid + '][]" value="' + value + '" />';
            $("#commit_id").append(html);
        })
    })
    $("#commit_id").submit();
}

/*****分录题js***/
function questionanswer_box_5(i) {
    var type = $("[name='questionid[" + i + "][type]']:checked").val();
    $("#questionanswer_box_5_" + i).find(".questionanswer_box_div").hide();
    $("#questionanswer_box_5_" + i).find("#questionanswer_box_5_" + type).show();
}
function del_questchoice(obj) {
    $(obj).parent().remove();
}
var lng = 0;
function add_select_5(i) {
    var type = $("#questionanswer_box_5_" + i).find("[name='questionanswer_5_type']:checked").val();
    var big_type = $("[name='questionid[" + i + "][type]']:checked").val();
    var addmoney = $("#questionanswer_box_5_" + i).find("#addmoney").val();
    var cate_id = $("#questionanswer_box_5_" + i).find(".area select option:selected");
    var big_money = $("[name='questionid[" + i + "][money]']").val();
    var type_str = ""
    if (type == 1) {
        type_str = "贷";
    } else {
        type_str = "借";
    }
    var cate_name = new Array();
    $.each(cate_id, function (i, v) {
        if ($(v).val() > 0) {
            cate_name.push($(v).text());
        }
    })
    cate_name = cate_name.join("-");
    if (cate_name == "") {
        alert("请选择科目");
        return false;
    }

    if (!(addmoney > 0)) {
        alert("金额要大于0");
        return false;
    }
    lng++;
    var key = i + "_" + lng;
    var code = $(cate_id).attr("code");
    //var text=$(cate_id).attr("cate_name")+"("+code+")"+"&nbsp;&nbsp;&nbsp;"+type_str+":"+addmoney+"元";
    var text = "<span class='s_left'>" + type_str + "&nbsp;&nbsp;:" + cate_name + "</span><span class='s_right'>" + addmoney + "元</span>";
    var html = ""
    html = '<div class="questchoice">';
    html += ' <div class="s_left_w">';
    html += text;
    html += '<input type="hidden" name="questionid[' + i + '][data][' + key + '][type]" value="' + type + '" />';
    html += '<input type="hidden" name="questionid[' + i + '][data][' + key + '][text]" value="' + text + '" />';
    html += '<input type="hidden" name="questionid[' + i + '][data][' + key + '][money]" value="' + addmoney + '" />';
    html += '<input type="hidden" name="questionid[' + i + '][data][' + key + '][code]" value="' + code + '" />';
    html += '</div>';
    html += '<a  onclick="del_questchoice(this)" >删除</a>';
    html += '<div class="clear"></div>';
    html += '</div>';
    $("#questionanswer_box_5_" + i).find("#business-wrapper-5").append(html);
    //	alert(addmoney);
    //alert(type_str);
}

function pev() {
    if (i > 0) {
        i--;
        show_qust(i);
    }
}

function next() {
    var l = $(".all_questions").size();
    if (i < l - 1) {
        i++;
        show_qust(i);
    }
}

function write_notes() {
    $("#detail_notes").find("pre").hide();
    var html = $("#detail_notes").find("pre").html();
    $("#detail_notes").find("#contentNoteDiv").show();
    $("#detail_notes").find("textarea").focus();
    $("#save_notes_bnt").show();
    $("#write_notes_bnt").hide();
}
function save_notes() {
    var html = $("#contentNote").val();
    $("#detail_notes").find("pre").html(html);
    $("#detail_notes").find("#contentNoteDiv").hide();
    $("#detail_notes").find("pre").show();
    $("#save_notes_bnt").hide();
    $("#write_notes_bnt").show();
    var knowsid = $("#knowsid").val();
    $.post("/Subject/save_note", {knowsid: knowsid, notes: html});
    dialog("保存成功");
}

function noti() {
    $.getJSON("/BoxApi/Index/noti", function (data) {
        var msgcount = data.msg_count;
        var list = data.list;
        if ($("#msg_noti_list_div").length > 0 && list != null) {
            box_render(list, $("#msg_noti_list_div").html(), $("#msg_noti_list"), "html");
        }

        if (msgcount != null && msgcount.comment > 0) {
            var text = "消息";
            $(".msg_remain_count").text(text + "(" + msgcount.comment + ")");
            $(".msg_remain_count").css("color", "red");
            if ($(".msg_remain_count_wap").length > 0) {
                //	$(".msg_remain_count_wap").text(msgcount.comment);
                $(".msg_remain_count_wap").show();
            }
        } else {
            $(".msg_remain_count_wap").hide();
        }

    })
}

$(function () {
    noti();
    setInterval(noti, 5000);

    $("img").error(function () {
        $(this).attr("src", "/Public/Images/0.jpg");
    })

    var user_info = app_init_data.user_info;
    set_shcool(user_info);
})


function jump_offline_select_course(_this) {
    var id = $(_this).val();
    hiApp.jump("/Subject/offline_select_course/subjectid/" + id);
    //	hiApp.get("/Subject/offline_select_course/subjectid/"+id,function(data){
//			  var content=$(data).find(".select_course_form").html();	
//						hiApp.hideIndicator();
//					$(".select_course_form").html(content);
//					CM.run("Subject_offline_select_courseCtrl","init");
//					//alert(content);//$(content);
//			});	
}
